package com.tubeonfire.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tubeonfire.entity.TubesFeed;
import com.tubeonfire.service.TubeService;
import com.tubeonfire.util.JavaCacheHandle;

public class TubeChartsController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static int itemPerPage = 12;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "public, max-age=86400");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		response.setHeader("Expires", cal.getTime().toString());
		// search by input key.
		try {
			String path = ((HttpServletRequest) request).getRequestURI();
			String[] splittedURI = path.split("/");
			int page = 1;
			String typeChart = "";
			if (splittedURI.length == 3) {
				typeChart = splittedURI[splittedURI.length - 1];

			} else if (splittedURI.length == 4) {
				typeChart = splittedURI[splittedURI.length - 2];
				try {
					System.out.println(splittedURI[splittedURI.length - 1]);
					page = Integer
							.parseInt(splittedURI[splittedURI.length - 1]);
				} catch (Exception e) {
					System.out.println("Error when parse page string to int.");
					page = 1;
				}
			} else {
				System.out.println("Error path.");
				response.sendRedirect("/home");
			}
			typeChart = URLDecoder.decode(typeChart, "UTF8");
			System.out.println("Chart type : " + typeChart);
			TubesFeed result = null;
			if (typeChart.equalsIgnoreCase("most-viewed")) {
				result = TubeService.getMostView("today", itemPerPage, page);
			} else if (typeChart.equalsIgnoreCase("most-shared")) {
				result = TubeService.getMostShared(itemPerPage, page);
			} else if (typeChart.equalsIgnoreCase("top-rated")) {
				result = TubeService.getTopRate("today", itemPerPage, page);
			} else if (typeChart.equalsIgnoreCase("most-discussed")) {
				result = TubeService.getMostDiscussed("today", itemPerPage, page);
			} else if (typeChart.equalsIgnoreCase("top-favorites")) {
				result = TubeService.getTopFavorites("today", itemPerPage, page);
			}
			if (result != null && result.getListTube().size() > 0) {
				request.setAttribute("url", request.getRequestURL());
				request.setAttribute("listCategory",
						JavaCacheHandle.getCategory());
				request.setAttribute("listChannel",
						JavaCacheHandle.getChannels());
				request.setAttribute("result", result.getListTube());
				request.setAttribute("typeChart", typeChart);
				request.setAttribute("totalResult", result.getTotalResult());
				System.out.println(result.getTotalResult());
				request.setAttribute("currentPage", result.getCurrentPage());
				request.getRequestDispatcher("/tube_charts.jsp").forward(request,
						response);
			} else {
				System.out.println("Null pointer.");
				response.sendRedirect("/home");
			}
		} catch (Exception e) {
			System.out.println("Exception.");
			e.printStackTrace();
			response.sendRedirect("/home");
		}
	}

}
